package com.baidu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baidu.constant.MessageConstant;
import com.baidu.entity.PageResult;
import com.baidu.entity.QueryPageBean;
import com.baidu.entity.Result;
import com.baidu.pojo.Permission;
import com.baidu.pojo.Role;
import com.baidu.service.PermissionService;
import com.baidu.service.RoleService;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;
    @Reference
    private PermissionService permissionService;

    @RequestMapping("/addRole")
    public Result addRole(@RequestBody Role role,Integer[] permissionIds){
        try {
            roleService.addRole(role,permissionIds);
            return new Result(true, "新增角色成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"新增角色失败");
        }
    }
    
    @RequestMapping("/findPermission")
    public Result findPermission(){
        try{
            List<Permission> roleList = (permissionService.findPermission());
            return new Result(true,null,roleList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询权限项失败");
        }
    }

    @RequestMapping("/findRoleById")
    public Result findRoleById(Integer id){
        try {
            Role role = roleService.findRoleById(id);
            return new Result(true,null,role);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }


    @RequestMapping("/findPermissionByRoleId")
    public Result findPermissionByRoleId(Integer id){
        try {
            Integer[] permissionIds = permissionService.findPermissionByRoleId(id);
            return new Result(true,null,permissionIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult=roleService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        return pageResult;
    }

    @RequestMapping("/editRole")
    public Result editRole(@RequestBody Role role,Integer[] permissionIds){
        try{
            roleService.editRole(role,permissionIds);
            return new Result(true,"修改角色信息成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"修改角色信息失败");
        }
    }

    @RequestMapping("/deleteRoleById")
    public Result deleteRoleById(Integer id){
        try{
            roleService.deleteRoleById(id);
            return new Result(true,"删除角色信息成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除角色信息失败");
        }
    }
}
